package cn.easybuy.servlet.product;

import cn.easybuy.entity.ProductCategory;
import cn.easybuy.service.ProductService;
import cn.easybuy.service.impl.ProductServiceImpl;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/cateServlet")
public class CateServlet extends HttpServlet {
    private ProductService productService = new ProductServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            int pid = Integer.parseInt(req.getParameter("pid"));
            int type = Integer.parseInt(req.getParameter("type"));
            List<ProductCategory> cate1 = productService.getCate1(pid, type);
            String jsonStr = JSONObject.toJSONString(cate1);
            System.out.println(jsonStr);
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter pw = resp.getWriter();
            pw.print(jsonStr);
            pw.flush();
            pw.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
